import { createRouter, createWebHistory } from 'vue-router'

// Auth/Login Views
import Login from '../views/auth/Login.vue'
import Register from '../views/auth/Register.vue'
import ForgotPassword from '../views/auth/ForgotPassword.vue'

// Admin Views
import AdminDashboard from '../views/admin/Dashboard.vue'
import UserManagement from '../views/admin/UserManagement.vue'
import DoctorManagement from '../views/admin/DoctorManagement.vue'
import DrugManagement from '../views/admin/DrugManagement.vue'
import ChecksManagement from '../views/admin/ChecksManagement.vue'
import ScheduleManagement from '../views/admin/ScheduleManagement.vue'
import DataVisualization from '../views/admin/DataVisualization.vue'

// Doctor Views
import DoctorDashboard from '../views/doctor/Dashboard.vue'
import PatientTreatment from '../views/doctor/PatientTreatment.vue'
import OrderHistory from '../views/doctor/OrderHistory.vue'

// Patient Views
import PatientDashboard from '../views/patient/Dashboard.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/forgot-password',
    name: 'ForgotPassword',
    component: ForgotPassword
  },
  // Admin routes
  {
    path: '/admin',
    name: 'AdminDashboard',
    component: AdminDashboard,
    meta: { requiresAuth: true, role: 'admin' },
    children: [
      {
        path: 'users',
        name: 'UserManagement',
        component: UserManagement
      },
      {
        path: 'doctors',
        name: 'DoctorManagement',
        component: DoctorManagement
      },
      {
        path: 'drugs',
        name: 'DrugManagement',
        component: DrugManagement
      },
      {
        path: 'checks',
        name: 'ChecksManagement',
        component: ChecksManagement
      },
      {
        path: 'schedule',
        name: 'ScheduleManagement',
        component: ScheduleManagement
      },
      {
        path: 'visualization',
        name: 'DataVisualization',
        component: DataVisualization
      }
    ]
  },
  // Doctor routes
  {
    path: '/doctor',
    name: 'DoctorDashboard',
    component: DoctorDashboard,
    meta: { requiresAuth: true, role: 'doctor' },
    children: [
      {
        path: 'treatment',
        name: 'PatientTreatment',
        component: PatientTreatment
      },
      {
        path: 'history',
        name: 'OrderHistory',
        component: OrderHistory
      }
    ]
  },
  // Patient routes
  {
    path: '/patient',
    name: 'PatientDashboard',
    component: PatientDashboard,
    meta: { requiresAuth: true, role: 'patient' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// Navigation guard
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('user')
  const userRole = isAuthenticated ? JSON.parse(localStorage.getItem('user')).role : null
  
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!isAuthenticated) {
      next({ name: 'Login' })
    } else if (to.meta.role && to.meta.role !== userRole) {
      next({ name: `${userRole.charAt(0).toUpperCase() + userRole.slice(1)}Dashboard` })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router 